<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 品牌 控制层
 * @author adela.xu 8.29
 */
class Brand extends My_Controller
{
    private $assign = array();
    /**
     * 默认方法
     * @author Toby.tu 2016-09-01
     */
    public function init(){
        $this->vendor('models/Brand_model','brand_model');
    }
    /**
     * 用户列表
     * @author Toby.tu 2016-08-30
     */
    public function index(){
        $brands = $this->brand_model->getAll();
        $this->assign['brands'] = $brands;
        $this->assign['view'] = 'admin/brand/index';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function create(){
        $this->assign['view'] = 'admin/brand/create';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增权限组
     * @author Toby.tu 2016-08-31
     */
    public function store(){
        try {
            $input = $this->input->post();
            if(!isset($input['brand'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['brand']['name'])){
                throw new Exception('请输入品牌名称。');
            }
            $brand = $this->brand_model->getOne(['name'=>$input['brand']['name']]);
            if(!empty($brand)){
                throw new Exception('品牌名称已存在。');
            }
            $param = $input['brand'];
            $param['create_time'] = $this->phpdate->time();
            $param['update_time'] = $param['create_time'];
            $brand_code = 'brand_code_'.$this->phpdate->mtime().$param['name'];//品牌码
            $param['code'] = $this->common->pwd($brand_code);//通过这个品牌码查询品牌数据
            $this->brand_model->newData($param);
            redirect('admin/brand/index');die;
        } catch (Exception $e) {
            $this->assign += $input['brand'];
            $this->assign['error_code'] = $e->getMessage();
            $this->assign['view'] = 'admin/brand/create';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function edit($id){
        $menu = $this->brand_model->getOne(['id'=>$id]);
        unset($menu['status']);
        $this->assign += $menu;
        $this->assign['id'] = $id;
        $this->assign['view'] = 'admin/brand/edit';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增权限组
     * @author Toby.tu 2016-08-31
     */
    public function update($id){
        try {
            $input = $this->input->post();
            if(!isset($input['brand'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['brand']['name'])){
                throw new Exception('请输入菜单姓名。');
            }
            $brand = $this->brand_model->getOne(['name'=>$input['brand']['name']]);
            if(!empty($brand) && $id != $brand['id']){
                throw new Exception('品牌名称已存在。');
            }
            $param = $input['brand'];
            $param['update_time'] = $this->phpdate->time();
            $this->brand_model->updateById($id,$param);
            redirect('admin/brand/index');die;
        } catch (Exception $e) {
            $this->assign += $input['brand'];
            $this->assign['id'] = $id;
            $this->assign['error_code'] = $e->getMessage();
            $this->assign['view'] = 'admin/brand/edit';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 禁用启用
     * @author Toby.tu 2016-08-31
     */
    public function enable($id){
        try {
            $input = $this->input->get();
            if(!isset($input['status'])){
                throw new Exception('参数不正确。');
            }
            $param = array();
            $param['status'] = ($input['status']==1)?0:1;
            $this->brand_model->updateById($id,$param);
            redirect('admin/brand/index');die;
        } catch (Exception $e) {
            redirect('admin/brand/index');die;
        }
    }
    /**
     * 删除
     * @author Toby.tu 2016-08-31
     */
    public function destroy($id){
        $this->user_model->deleteById($id);
        redirect('admin/User/index');die;
    }
}
